/*
 * @Author: Oscar Pena
 * 
 * */
package FlowGram.Application.SimulatorWindow;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

import  FlowGram.Application.Parser.FinchProgramFlow;
import  FlowGram.Application.SimulatorWindow.Simulator.SimulationController;
import  FlowGram.Application.SimulatorWindow.Simulator.SimulatorEngineWindow;
import  FlowGram.Application.SimulatorWindow.SimulatorInterface.User;
import  FlowGram.Application.SimulatorWindow.TerrainEditor.TerrainEditorWindow;

public class SimulationWindow extends JFrame implements ActionListener, ItemListener {
	
	
	final static String SIMULATORPANEL = "Show Simulator Window";
	final static String TERRAINEDITOR = "Show Terrain Editor";
	public JButton ShowEditor = new JButton(TERRAINEDITOR);
	public JButton ShowSimulator = new JButton(SIMULATORPANEL);
	public SimulatorEngineWindow sim = new SimulatorEngineWindow();
	public JPanel topPanel = new JPanel();
	public User currentUser;
	private	JSplitPane	splitPaneV;
	private boolean isSimActive = true;

	

	
	public SimulationWindow() // the constructor
		{
		super("Simulator Window");
		super.setDefaultLookAndFeelDecorated(true);
		try {
			//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// Get the size of the default screen
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int w = dim.width;
		int h =	dim.height;
		//reduces the width and hight by 3%.
		w = (int) (w - (w * 0.3));
		h = (int) (h - (h * 0.3));
		//sets frame size
		setSize(w,h);
		//setLayout(new BorderLayout());
		// topPanel height is max( (h/9), 90);
		int topPanelheight = h/8;
		if (topPanelheight < 120){ topPanelheight = 120;}
		topPanel.setSize(w, topPanelheight);
		topPanel.add(ShowEditor);
		ShowEditor.addActionListener(this);
		topPanel.add(ShowSimulator);
		ShowSimulator.addActionListener(this);

		splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
		splitPaneV.setTopComponent(topPanel);
		splitPaneV.setBottomComponent(sim);
		

		sim.setVisible(true);
		add(splitPaneV);
		}
	@Override
	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub
		//System.out.println("item");
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		//System.out.println(arg0.getActionCommand());
		SwapWindows(arg0.getActionCommand());


	}

	private void SwapWindows(String string) {


		if (TERRAINEDITOR.equals(string)){
			
			if (isSimActive==true){
				super.setTitle("Terrain Editor: ");
				sim.settext("Terrain Editor Mode activated");
				sim.showEditorButtons();
				isSimActive = false;
			}
			repaint();
		} else if (SIMULATORPANEL.equals(string)){
			
			if (isSimActive==false){
				sim.settext("Simulation Mode activated");
				super.setTitle("Simulator Window");
				sim.showSinButtons();
				isSimActive = true;
			}
			repaint();
		}
	}
	public void set_text(Object t){
		//SimulatorPanel.settext(t);
		this.setTitle((String) t);
	}
	public String get_my_text(){
		
		//return SimulatorPanel.get_text();
		return null;
	}
     public void setTerrain(String string){
     
       sim.setTerrain(string);
     
     }
	public void set_simulation(FinchProgramFlow flow) {
		// TODO Auto-generated method stub
		sim.set_simulation(flow);
		}

}